package net.qhzw.vip.qiyu.timer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.qhzw.vip.qiyu.entity.MaintRpt;
import net.qhzw.vip.qiyu.entity.SysCommunity;
import net.qhzw.vip.qiyu.feign.CommunityFeignClient;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.MaintRptService;
import net.qhzw.vip.qiyu.util.CaculateDistance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/***
 * 维修派单
 */
@Component
public class RepairOrderTask {

    @Autowired
    private MaintRptService maintRptService;

    @Autowired
    private CommunityFeignClient communityFeignClient;

    /****
     * 每30秒执行一次 查询维修列表
     */
    //@Scheduled(cron = "0/30 * * * * ?")
    public void loadGoodsPushRedis() {
        List<MaintRpt> list = maintRptService.list(new LambdaQueryWrapper<MaintRpt>()
                .eq(MaintRpt::getStatus, 1).last("ORDER BY created DESC"));
        if (list.isEmpty())
            return;
        JsonResult<List<SysCommunity>> listJsonResult = communityFeignClient.queryBox((byte) 1, null, (byte) 2);
        List<SysCommunity> data = listJsonResult.getData();
        //分配维修任务
        assignMaint(list, data);
    }

    /***
     * 分配维修任务
     * @param maintRpts
     * @param communities
     */
    private void assignMaint(List<MaintRpt> maintRpts, List<SysCommunity> communities) {
        if (CollectionUtils.isEmpty(maintRpts)) {
            return;
        }
        if (CollectionUtils.isEmpty(communities)) {
            maintRpts.forEach(data -> saveQueue(data));
            return;
        }

        for (SysCommunity community : communities) {
            communityMaint(community, maintRpts);
        }

    }

    /***
     * 根据站点距离分配维修任务
     */
    public void communityMaint(SysCommunity community, List<MaintRpt> maintRpts) {
        for (MaintRpt maintRpt : maintRpts) {
            //根据经纬度计算距离
            double distance = CaculateDistance.LantitudeLongitudeDist(maintRpt.getLongitude(), maintRpt.getLatitude(), community.getLongitude(), community.getLatitude());
            //站点辐射距离
            Double radDistance = community.getRadDistance() * 1000;
            if (radDistance <= distance) {
                assignSite(community.getId(), maintRpt);
                continue;
            } else {
                saveQueue(maintRpt);
            }
        }

    }


    public void test(List<MaintRpt> maintRpts, List<SysCommunity> communities) {
        for (MaintRpt maintRpt : maintRpts) {
            for (SysCommunity community : communities) {
                //根据经纬度计算距离
                double distance = CaculateDistance.LantitudeLongitudeDist(maintRpt.getLongitude(), maintRpt.getLatitude(),
                        community.getLongitude(), community.getLatitude());
                //站点辐射距离
                Double radDistance = community.getRadDistance() * 1000;
                if (distance >= radDistance) {
                    //分配站点
                    assignSite(community.getId(), maintRpt);
                } else {
                    //存入队列
                    saveQueue(maintRpt);
                }
            }
        }
    }


    /**
     * 站点分配维修任务
     *
     * @param id
     * @param maintRpt
     */
    private void assignSite(Long id, MaintRpt maintRpt) {
        System.out.println("分配给该站点");

    }

    /***
     * 存入维修队列
     * @param maintRpt
     */
    private void saveQueue(MaintRpt maintRpt) {
        System.out.println("存入维修队列");

    }


    public static void main(String[] args) {
        double v = CaculateDistance.LantitudeLongitudeDist(101.752275, 36.642517, 61.1, 1.2);

        System.out.println(v);
    }


}
